package com.acwing.partition13;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/3/6 16:54
 */
public class AC1220生命之树 {

    private static final int N = 100010;
    private static int[] h = new int[N], w = new int[N], e = new int[2 * N], ne = new int[2 * N];
    private static long[] f = new long[N];
    private static int n, idx = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(reader.readLine());
        String[] ss = reader.readLine().split(" ");
        for (int i = 1; i <= n; i++) w[i] = Integer.parseInt(ss[i - 1]);
        Arrays.fill(h, -1);
        for (int i = 0; i < n - 1; i++) {
            ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]);
            add(a, b);
            add(b, a);
        }
        dfs(1, -1);
        long ans = (long) -1e18;
        for (int i = 1; i <= n; i++) ans = Math.max(ans, f[i]);
        writer.write(ans + "\n");
        writer.flush();
    }

    private static void dfs(int u, int parent) {
        f[u] = w[u];
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if (v == parent) continue;
            dfs(v, u);
            f[u] = Math.max(f[u], f[u] + f[v]);
        }
    }

    private static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}
